﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormSubstantialWaterColumnCalculation : System.Windows.Forms.Form
	{
		public FormSubstantialWaterColumnCalculation()
		{
			this.InitializeComponent();
			base.CenterToScreen();
			//base.Icon = Resource.YArchitechIcon;
		}

		private void FormSubstantialWaterColumnCalculation_Load(object sender, EventArgs e)
		{
			this.comboBoxGunDiameter.Items.Add("13");
			this.comboBoxGunDiameter.Items.Add("16");
			this.comboBoxGunDiameter.Items.Add("19");
			this.comboBoxFireDiameter.Items.Add("50");
			this.comboBoxFireDiameter.Items.Add("65");
			this.comboBoxFireDiameter.Items.Add("80");
			this.comboBoxFireMatireals.Items.Add("麻织");
			this.comboBoxFireMatireals.Items.Add("帆布");
			this.comboBoxFireMatireals.Items.Add("衬胶");
			this.comboBoxFireLength.Items.Add("20");
			this.comboBoxFireLength.Items.Add("25");
			string[] substantialWaterColumnCalculationText = this.XML.GetSubstantialWaterColumnCalculationText();
			this.comboBoxGunDiameter.SelectedItem = substantialWaterColumnCalculationText[0];
			this.comboBoxFireDiameter.SelectedItem = substantialWaterColumnCalculationText[1];
			this.isTrue = true;
			this.comboBoxFireMatireals.SelectedItem = substantialWaterColumnCalculationText[2];
			this.comboBoxFireLength.Text = substantialWaterColumnCalculationText[3];
			this.textBoxWaterLevels.Text = substantialWaterColumnCalculationText[4];
			this.buttonTXT.Enabled = false;
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			try
			{
				double ad = double.Parse(this.textBoxFireDrag.Text);
				double ld = double.Parse(this.comboBoxFireLength.Text.ToString());
				double qxh = double.Parse(this.textBoxWaterLevels.Text);
				double b = this.helper.getB(this.comboBoxGunDiameter.SelectedItem.ToString());
				double value = this.helper.getFirePressure(ad, ld, qxh, b);
				value = Math.Round(value, 3);
				this.textBoxFirePressure.Text = value.ToString();
				double num = this.helper.gethq(qxh, b);
				double f = this.helper.getf(this.comboBoxGunDiameter.SelectedItem.ToString());
				double value2 = this.helper.getSubstantialWaterColumn(num * 10.0, f);
				value2 = Math.Round(value2, 3);
				this.textBoxWaterLength.Text = value2.ToString();
				this.SaveUserText();
				this.buttonTXT.Enabled = true;
			}
			catch
			{
				MessageBox.Show("请确保输入不为空并且输入为数字！");
			}
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonTXT_Click(object sender, EventArgs e)
		{
			this.SaveFileDialog();
		}

		private void SaveFileDialog()
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "文本文件|*.txt";
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				string text = saveFileDialog.FileName.ToString();
				string str = text.Substring(text.LastIndexOf("\\") + 1);
				text.Substring(0, text.LastIndexOf("\\"));
                //DateTime.Now.ToString("yyyyMMdd") + str;
				saveFileDialog.FileName.Insert(1, "dameng");
				this.WriteTXT(text);
				if (MessageBox.Show("是否现在查看？", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					Process.Start(text);
				}
			}
		}

		private void WriteTXT(string path)
		{
			FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine("");
			streamWriter.WriteLine("                         消火栓系统充实水柱计算");
			streamWriter.WriteLine("  已知条件：");
			streamWriter.WriteLine("      水枪喷嘴口径(mm)： " + this.comboBoxGunDiameter.SelectedItem.ToString());
			streamWriter.WriteLine("      水带材料: " + this.comboBoxFireMatireals.SelectedItem.ToString());
			streamWriter.WriteLine("      水带口径(mm)： " + this.comboBoxFireDiameter.SelectedItem.ToString());
			streamWriter.WriteLine("      Ld： 水龙带长度(m): " + this.comboBoxFireLength.Text.ToString());
			streamWriter.WriteLine("      Ad： 水带比阻： " + this.textBoxFireDrag.Text.ToString());
			streamWriter.WriteLine("      B：  水枪水流特征系数： " + this.helper.getB(this.comboBoxGunDiameter.SelectedItem.ToString()));
			streamWriter.WriteLine("      Qxh：消火栓出水量(L/S)： " + this.textBoxWaterLevels.Text.ToString());
			streamWriter.WriteLine("");
			streamWriter.WriteLine("  计算公式：");
			streamWriter.WriteLine("");
			streamWriter.WriteLine("      消火栓口水压(m·h2O)：Hxh = Hd + Hq = Ad×Ld×Qxh×Qxh ＋ Qxh×Qxh / B");
			streamWriter.WriteLine("");
			streamWriter.WriteLine("  计算过程：");
			streamWriter.WriteLine("");
			streamWriter.WriteLine("      查设计手册 充实水柱长Sk, 水枪喷嘴出流量Qxh与水枪喷嘴所需水压关系图  Sk=" + this.textBoxWaterLength.Text);
			streamWriter.WriteLine(string.Concat(new object[]
			{
				"      Hxh = ",
				this.textBoxFireDrag.Text.ToString(),
				"*",
				this.comboBoxFireLength.Text.ToString(),
				"*",
				this.textBoxWaterLevels.Text,
				"*",
				this.textBoxWaterLevels.Text,
				"+",
				this.textBoxWaterLevels.Text,
				"*",
				this.textBoxWaterLevels.Text,
				"/",
				this.helper.getB(this.comboBoxGunDiameter.SelectedItem.ToString())
			}));
			streamWriter.WriteLine("");
			streamWriter.WriteLine("          = " + this.textBoxFirePressure.Text);
			streamWriter.Close();
			fileStream.Close();
		}

		private void SaveUserText()
		{
			string[] texts = new string[]
			{
				this.comboBoxGunDiameter.SelectedItem.ToString(),
				this.comboBoxFireDiameter.SelectedItem.ToString(),
				this.comboBoxFireMatireals.SelectedItem.ToString(),
				this.comboBoxFireLength.Text,
				this.textBoxWaterLevels.Text.ToString()
			};
			this.XML.WriteSubstantialWaterColumnCalculationText(texts);
		}

		private void comboBoxFireDiameter_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.isTrue)
			{
				this.textBoxFireDrag.Text = SubstantialWaterColumnCalculationHelper.getTxt(this.comboBoxFireDiameter.SelectedItem.ToString(), this.comboBoxFireMatireals.SelectedItem.ToString());
			}
		}

		private void comboBoxFireMatireals_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.isTrue)
			{
				this.textBoxFireDrag.Text = SubstantialWaterColumnCalculationHelper.getTxt(this.comboBoxFireDiameter.SelectedItem.ToString(), this.comboBoxFireMatireals.SelectedItem.ToString());
			}
		}

		private HVACUserSetting XML = new HVACUserSetting();

		private SubstantialWaterColumnCalculationHelper helper = new SubstantialWaterColumnCalculationHelper();

		private bool isTrue;
	}
}
